package com.learn.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.learn.bean.Product;
import com.learn.common.Response;
import com.learn.mapper.ProductMapper;

@RestController
@RequestMapping("/product")
public class ProductController {

	@Autowired
	private ProductMapper productMapper;
	
	@PostMapping("/add")
	public Response add(Product product){
		Integer result = productMapper.add(product);
		if(result == null){
			return new Response(500,"fail");
		}
		return new Response(200,"ok");
	}
	
	@PutMapping("/update")
	public Response update(Product product){
		Integer result = productMapper.update(product);
		if(result == 0){
			return new Response(500,"fail");
		}
		return new Response(200,"ok");
	}
	
	@DeleteMapping("/{id}")
	public Response delete(@PathVariable("id")Integer id){
		Integer result = productMapper.deleteById(id);
		if(result == 0){
			return new Response(500,"fail");
		}
		return new Response(200,"ok");
	}
	
	@GetMapping("/{id}")
	public Response get(@PathVariable("id")Integer id){
		Product product = productMapper.getById(id);
		return new Response(200,"ok",product);
	}
	
	@GetMapping("/list")
	public Response list(){
		List<Product> list = productMapper.queryList();
		return new Response(200,"ok",list);
	}
}
